<template>
	<view>
		<empty v-if='cartList.length===0'></empty>
		<cart-item ref='cart' v-else :cartList="cartList" onRemoveCart="onRemoveCart"></cart-item>
	</view>
</template>

<script>
	import empty from '@/components/devm/cart/empty.vue'
	import cartItem from '@/components/devm/cart/cart-item.vue'
	export default {
		components: {
			empty,
			cartItem
		},
		data() {
			return {
				cartList: []
			};
		},
		onLoad() {
		},
		onShow() {
			this.initCartData()
			//每次都刷新tabbar购物车数量 
			this.initCartNum();
		},
		methods: {
			initCartNum() {
				var that = this;
				this.$get('mall/cart/count', {}).then((res) => {
					if (res.data) {
						that.$global.changCartNum(res.data)//刷新全局变量
						uni.setTabBarBadge({
							index: 2,
							text: this.$global.data.cartNum + ''
						})
					}
				})
			},
			onRemoveCart(itemlen,index){
				if(itemlen==0) {
					this.cartList = [];
				}
				this.$global.changCartNum(this.$global.data.cartNum-1)//刷新全局变量
				this.refreshCartBadge();
			},
			initCartData() {
				var that = this;
				this.$get('mall/cart/commonlist', {}).then((res) => {
					if (res.data) {
						for(var k=0;k<res.data.length;k++) {
							res.data[k].checked=true;
						}
						that.cartList = res.data;
					}
				})
			},

		}
	}
</script>

<style>
	page {
		background-color: #FFFFFF;
	}
</style>
